package com.msa.msaportal.web.listener;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import com.msa.msaportal.common.web.ConstantUntil;
import com.msa.msaportal.common.web.FileUtil;


public class FileDealContextListener implements ServletContextListener {
	
	

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		System.out.println("-------Destroy-----------");

	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		System.out.println("-------------init file--------------------");
		//服务器文件目录
		String path=getClass().getResource("/").getFile().toString().replace("/WEB-INF/classes/", "/portalfront/");
		File backupFile=new File(ConstantUntil.BACKUPCOPY_PATH);
		
		try {
			path=URLDecoder.decode(path,"utf-8");
			//查看本地磁盘目录是否存在，1不存在就创建；2存在就复制本地目录文件到服务器下面
			if(!backupFile.exists()){
				backupFile.mkdirs();
				File backupImgFile=new File(ConstantUntil.BACKUPIMGE_URL);
				File backupJspFile=new File(ConstantUntil.BACKUPJSP_URL);
				File backupCssFile=new File(ConstantUntil.BACKUPSTYLE_URL);
				backupImgFile.mkdirs();
				backupJspFile.mkdirs();
				backupCssFile.mkdirs();
				//服务器底下文件备份到本地
				FileUtil.copyDirectory(path, ConstantUntil.BACKUPCOPY_PATH, true);
				
			}else{
				//备份文件导入到服务器下面
				boolean copy = FileUtil.copyDirectory(ConstantUntil.BACKUPCOPY_PATH, path, true);
				if(copy){
					System.out.println("备份文件导入成功");
				}else{
					System.out.println("备份文件导入失败");
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			System.out.println("error: "+e.getMessage());
		}
	}

}
